<?php
/**
 * 七牛云控制类
 * @author 大梦
 * @DateTime 2019/07/06
 */

namespace app\api\controller;

use think\Controller;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class Qiniu extends Controller
{

    function up_token()
    {

        header("Access-Control-Allow-Origin: *");
        header("Content-Type:application/json");
        header('Access-Control-Allow-Methods:GET, POST, OPTIONS');
        header('Access-Control-Allow-Credentials:false');
        if (strtoupper($_SERVER['REQUEST_METHOD']) == 'OPTIONS') {
            exit;
        }

        $accessKey = get_system_config('qiniu_access_key', 'system');
        $secretKey = get_system_config('qiniu_secret_key', 'system');
        $bucket    = get_system_config('qiniu_bucket', 'system');
        $domain    = get_system_config('qiniu_domain', 'system');
        $pipeline  = get_system_config('qiniu_pipeline', 'system');

        $auth = new Auth($accessKey, $secretKey);

        $saveMp4Entry  = \Qiniu\base64_urlSafeEncode($bucket . ":$(key).mp4");
        $saveJpgEntry  = \Qiniu\base64_urlSafeEncode($bucket . ":$(key).jpg");
        $avthumbMp4Fop = "avthumb/mp4|saveas/" . $saveMp4Entry;
        $vframeJpgFop  = "vframe/jpg/offset/1|saveas/" . $saveJpgEntry;

        $options['persistentOps']      = $avthumbMp4Fop . ";" . $vframeJpgFop;
        $options['persistentPipeline'] = $pipeline;

        $options['scope']      = $bucket;
        $options['saveKey']    = '$(etag)$(ext)';
        $options['returnBody'] = '{"key":"$(key)","name":"$(fname)","save_path":"$(key)","hash":"$(etag)","fsize":"$(fsize)","type":"$(mimeType)","ext":"$(ext)"}';

        $expires = 3600;

        $upToken = $auth->uploadToken($bucket, null, $expires, $options, true);

        $return['uptoken'] = $upToken;
        $return['domain']  = $domain;

        return json($return);
    }

    function js_upload()
    {
        $request = $this->request;

        $data['name']      = $request->name;
        $data['is_cloud']  = 1;
        $data['md5']       = $request->hash;
        $data['size']      = $request->fsize;
        $data['type']      = $request->type;
        $data['save_path'] = $request->save_path;

        // 去掉最前面的.
        $data['ext'] = str_replace(".", "", $request->ext);

        $file = model('File')->create($data);
        $file->append(['sid', 'url'])->hidden(['id']);

        // Return Success JSON-RPC response
        $return['code'] = 1;
        $return['data'] = $file;

        return $return;

    }

    function ueditor_upload()
    {
        $accessKey = get_system_config('qiniu_access_key', 'system');
        $secretKey = get_system_config('qiniu_secret_key', 'system');
        $bucket    = get_system_config('qiniu_bucket', 'system');
        $domain    = get_system_config('qiniu_domain', 'system');
        $pipeline  = get_system_config('qiniu_pipeline', 'system');

        $auth = new Auth($accessKey, $secretKey);

        $saveMp4Entry  = \Qiniu\base64_urlSafeEncode($bucket . ":$(key).mp4");
        $saveJpgEntry  = \Qiniu\base64_urlSafeEncode($bucket . ":$(key).jpg");
        $avthumbMp4Fop = "avthumb/mp4|saveas/" . $saveMp4Entry;
        $vframeJpgFop  = "vframe/jpg/offset/1|saveas/" . $saveJpgEntry;

        $options['persistentOps']      = $avthumbMp4Fop . ";" . $vframeJpgFop;
        $options['persistentPipeline'] = $pipeline;

        $options['scope']      = $bucket;
        $options['saveKey']    = '$(etag)$(ext)';
        $options['returnBody'] = '{"key":"$(key)","name":"$(fname)","save_path":"$(key)","hash":"$(etag)","fsize":"$(fsize)","type":"$(mimeType)","ext":"$(ext)"}';

        $expires = 3600;

        $upToken = $auth->uploadToken($bucket, null, $expires, $options, true);

        $uploadMgr = new UploadManager();

        $request = $this->request->param();

        $ext = get_file_ext_name($request['name']);

        $key = date('Ym') . '/' . md5_file($request['tmp_name']) . '.' . $ext;
        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($upToken, $key, $request['tmp_name']);
        if ($err !== null) {
            return false;
        } else {

            $data['name']      = $ret['name'];
            $data['is_cloud']  = 1;
            $data['md5']       = $ret['hash'];
            $data['size']      = $ret['fsize'];
            $data['type']      = $ret['type'];
            $data['save_path'] = $ret['save_path'];

            // 去掉最前面的.
            $data['ext'] = $ext;

            $file = model('File')->create($data);

            return $domain . '/' . $data['save_path'];
        }
    }
}
